##
# variant.py - Created by Timothy Morey on 6/10/2012
#
# This file provides a ctypes wrapper for libnvn items defined in variant.h.
#

import ctypes

# VariantType enum values, which also line up with the NetCDF nc_type enum.
VariantTypeNull = 0
VariantTypeByte = 1
VariantTypeChar = 2
VariantTypeShort = 3
VariantTypeInt = 4
VariantTypeFloat = 5
VariantTypeDouble = 6

class VariantValue(ctypes.Union):
    _fields_ = [
                ('ByteVal',   ctypes.c_ubyte),
                ('CharVal',   ctypes.c_char),
                ('ShortVal',  ctypes.c_short),
                ('IntVal',    ctypes.c_int),
                ('FloatVal',  ctypes.c_float),
                ('DoubleVal', ctypes.c_double),
               ]

class Variant(ctypes.Structure):
    _fields_ = [
                ('Type',  ctypes.c_int),
                ('Value', VariantValue),
               ]

def CreateVariant(val):

    if type(val) == int:
        return Variant(Type = VariantTypeInt,
                       Value = VariantValue(IntVal = val))

    elif type(val) == float:
        return Variant(Type = VariantTypeFloat,
                       Value = VariantValue(FloatVal = val))

    else:
        print "CreateVariant doesn't support", type(val)
        return Variant(Type = VariantTypeNull)
